package org.openmore.platform.service.impl;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.openmore.common.exception.OpenmoreException;
import org.openmore.platform.dao.PlatformConfigMapper;
import org.openmore.platform.dto.api.PlatformConfigDto;
import org.openmore.platform.entity.PlatformConfig;
import org.openmore.platform.entity.enums.PlatformType;
import org.openmore.platform.service.BaseServiceImpl;
import org.openmore.platform.service.PlatformConfigService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class PlatformConfigServiceImpl extends BaseServiceImpl implements PlatformConfigService {

    @Autowired
    private PlatformConfigMapper platformConfigMapper;
    @Override
    public PlatformConfig getEntityById(String id) {
        Example example = new Example(PlatformConfig.class);
        example.createCriteria().andEqualTo("id", id).andEqualTo("deleted", false);
        return platformConfigMapper.selectOneByExample(example);
    }

    @Override
    public List<PlatformConfig> selectAll(String tenantId, PlatformType platformType, String appid, String mchKey) {
       Example example = new Example(PlatformConfig.class);
       Example.Criteria criteria =  example.createCriteria().andEqualTo("tenantId", tenantId)
               .andEqualTo("deleted", false);

       if(!StringUtils.isEmpty(tenantId)){
           criteria.andEqualTo("tenantId", tenantId);
       }
       if(null != platformType){
           criteria.andEqualTo("platformType", platformType);
       }
       if(!StringUtils.isEmpty(appid)){
           criteria.andEqualTo("appid", appid);
       }
       if(!StringUtils.isEmpty(mchKey)){
           criteria.andEqualTo("mchKey", mchKey);
       }
        return platformConfigMapper.selectByExample(example);
    }

    @Override
    public Integer selectCount(String tenantId, PlatformType platformType, String appid, String mchKey) {
        Example example = new Example(PlatformConfig.class);
        Example.Criteria criteria =  example.createCriteria().andEqualTo("tenantId", tenantId)
                .andEqualTo("deleted", false);

        if(!StringUtils.isEmpty(tenantId)){
            criteria.andEqualTo("tenantId", tenantId);
        }
        if(null != platformType){
            criteria.andEqualTo("platformType", platformType);
        }
        if(!StringUtils.isEmpty(appid)){
            criteria.andEqualTo("appid", appid);
        }
        if(!StringUtils.isEmpty(mchKey)){
            criteria.andEqualTo("mchKey", mchKey);
        }
        Integer count = platformConfigMapper.selectCountByExample(example);
        count = null==count?0: count;
        return count;
    }

    @Override
    public PlatformConfig insert(PlatformConfigDto platformConfig) {
        if(null == platformConfig){
            throw new OpenmoreException(400, "参数不能为空");
        }
        if(StringUtils.isEmpty(platformConfig.getTenantId())){
            throw new OpenmoreException(400, "租户key不能为空");
        }
        if(null == platformConfig.getPlatformType()){
            throw new OpenmoreException(400, "账户类型不能为空");
        }
        if(platformConfig.getPlatformType() == PlatformType.WECHAT_PAY && StringUtils.isEmpty(platformConfig.getMchKey())){
            throw new OpenmoreException(400, "微信商户key不能为空");
        }else if(StringUtils.isEmpty(platformConfig.getAppid())){
            throw new OpenmoreException(400, "appId不能为空");
        }
        List<PlatformConfig> platformConfigs = selectAll(platformConfig.getTenantId(), platformConfig.getPlatformType(), null, null);
        if(!CollectionUtils.isEmpty(platformConfigs)){
            throw new OpenmoreException(400, "已存在此账户如需变更请查询更改");
        }
        PlatformConfig newPlatformConfig = new PlatformConfig();
        BeanUtils.copyProperties(platformConfig, newPlatformConfig);
        beforeInsert(newPlatformConfig);
        platformConfigMapper.insert(newPlatformConfig);
        return newPlatformConfig;
    }

    @Override
    public void deleteById(String id) {
        if(StringUtils.isEmpty(id)){
            throw new OpenmoreException(400, "参数不能为空");
        }
        PlatformConfig platformConfig = getEntityById(id);
        platformConfig.setDeleted(true);
        beforeUpdate(platformConfig);
        Example example = new Example(PlatformConfig.class);
        platformConfigMapper.updateByExampleSelective(platformConfig, example);
    }

    @Override
    public void update(PlatformConfigDto platformConfig) {
        if(null == platformConfig || StringUtils.isEmpty(platformConfig.getId())){
            throw new OpenmoreException(400, "参数不能为空");
        }
        PlatformConfig old = getEntityById(platformConfig.getId());
        if(platformConfig.getPlatformType() != old.getPlatformType()){
            throw new OpenmoreException(400, "账户类型不允许修改");
        }
        PlatformConfig newPlatformConfig = new PlatformConfig();
        BeanUtils.copyProperties(platformConfig, newPlatformConfig);
        beforeUpdate(newPlatformConfig);
        Example example = new Example(PlatformConfig.class);
        example.createCriteria().andEqualTo("id", platformConfig.getId());
        platformConfigMapper.updateByExampleSelective(newPlatformConfig, example);

    }
}
